package com.draven.management.controller;

import com.draven.common.BaseController;
import com.draven.constants.FramworkConstants;
import com.draven.entity.Module;
import com.draven.entity.User;
import com.draven.service.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Draven on 2017/4/11.
 * E-mail:draven1122@163.com
 */
@Controller
@RequestMapping("/management")
public class CommonController extends BaseController {
    @Autowired
    private ModuleService moduleService;

    @RequestMapping("")
    public ModelAndView main(HttpServletRequest request) {
        User user =getSessionValue(request,FramworkConstants.USER_SESSION_KEY);
        ModelAndView modelAndView = new ModelAndView("/management/common/index");
        modelAndView.addObject("realname",user.getRealname());
        return modelAndView;
    }

    /**
     * 读取后台首页功能菜单
     *
     * @return
     */
    @RequestMapping("/modules")
    @ResponseBody
    public String modules() {
        String jsonString = "[]";
        try {
            List<Module> modules = moduleService.listModulesByParentId("0");
            jsonString = listToJsonArrayStr(modules);
            return jsonString;
        } catch (Exception e) {
            e.printStackTrace();
            return jsonString;
        }
    }
}
